<?php
include_once( "common.php" );
checkLogin( STATUS_GUEST, $_GET["id"] );

# See if the bug is valid and the user can see it
if ( ! ( isValidEntry( "Bug", $_GET["id"] ) and canSeeBug( $_GET["id"] ) ) )
    outputAlert( "Invalid input", "You have entered invalid input." );

outputHeader();

# Get information on the current bug
$result = db_query( "SELECT Bug.* FROM Bug WHERE Bug.id = {$_GET["id"]}" );
$row = db_fetch_object( $result );

# User is allowed to update bugs
if ( hasStatus( STATUS_USER ) )
{
?>
<div class = "header">Edit bug</div>
<form action = "editBug.php" method = "post">
<table>
<tr>
<td>Project</td><td><?php outputField( "Project", "project", getNameByID( "Project", $row->project ) ); ?></td>
<td>Assigned</td><td><?php outputField( "User", "assigned", getNameByID( "User", $row->assigned ) ); ?></td>
<td>Priority</td><td><?php outputField( "Priority", "priority", getNameByID( "Priority", $row->priority ) ); ?></td>
<td>Status</td><td><?php outputField( "Status", "status", getNameByID( "Status", $row->status ) ); ?></td>
</tr>
<tr><td>Description</td><td colspan = "7"><strong><?php print stripslashes( $row->description ); ?></strong></td></tr>
<tr><td>Comment</td><td colspan = "7"><textarea name = "comment" rows = "3" cols = "40"></textarea></td></tr>
<tr>
<td><input type = "hidden" name = "id" value = "<?php print $row->id; ?>" /></td><td><input type = "submit" value = "File bug report" /></td>
<?php
    if ( hasStatus( STATUS_ADMIN ) )
        print "<td><p class = 'link'><a href = 'deleteBug.php?id={$row->id}'>Delete bug</a></p></td>\n";
?>
</tr>
</table>
</form>
<div class = "header">Logs</div>
<?php
}

# User can only view bugs
else
{
?>
<div class = "header">Bug details</div>
<table>
<tr>
<td>Project</td><td><strong><?php print getNameByID( "Project", $row->project ) ?></strong></td>
<td>Assigned</td><td><strong><?php print getNameByID( "User", $row->assigned ) ?></strong></td>
<td>Priority</td><td><strong><?php print getNameByID( "Priority", $row->priority ) ?></strong></td>
<td>Status</td><td><strong><?php print getNameByID( "Status", $row->status ) ?></strong></td>
</tr>
<tr><td>Description</td><td colspan = "7"><strong><?php print stripslashes( $row->description ); ?></strong></td></tr>
</table>
<div class = "header">Logs</div>
<?php
}
outputLogLinks( $_GET["id"] );

# Which page are we showing?
$limit = " LIMIT 0, " . LOGS_PER_PAGE;
if ( is_numeric( $_GET["page"] ) and $_GET["page"] > 0 )
{
    $limit = " LIMIT " . ( $_GET["page"] - 1 ) * LOGS_PER_PAGE . ", " . LOGS_PER_PAGE;
}

# Get all logs
$result = db_query( "SELECT Log.* FROM Log WHERE Log.bug = {$_GET["id"]} AND Log.initial = 0 ORDER BY Log.timestamp DESC{$limit}" );

# There are no logs
if ( db_num_rows( $result ) == 0 )
    print "<p class = 'comment'>No logs.</p>\n";

# There are logs
else
{
    # Set up an array to map column entries to table names
    $table_match = array();
    $table_match["project"] = "project";
    $table_match["assigned"] = "user";
    $table_match["priority"] = "priority";
    $table_match["status"] = "status";

    while ( $row = db_fetch_object( $result ) )
    {
        # Parse any changes to the bug
        if ( $row->initial == 0 )
        {
            # Get the user's name
            $row->user = getNameByID( "User", $row->user );

            # Format the date in a pretty way
            $row->timestamp = formatTimestamp( $row->timestamp );

            print "<div class = 'comment'>\n";
            print "<p><strong>{$row->user}</strong>, {$row->timestamp}:</p>\n";

            # Output the comment if there is one
            if ( $row->comment )
            {
                $row->comment = stripslashes( $row->comment );
                print "<p class = 'comment'>{$row->comment}</p>\n";
            }

            # Figure out any changes that were made
            foreach ( array_keys( $table_match ) as $field )
            {
                if ( $row->$field != -1 )
                {
                    print "<p class = 'change'>" . ucfirst( $field ) . ": " . getNameByID( ucfirst( $table_match[$field] ), $row->$field ) . "</p>\n";
                }
            }

            # The user is allowed to delete this comment
            if ( $row->user == $_SESSION["user"] or hasStatus( STATUS_ADMIN ) )
                print "<p class = 'link'><a href = 'deleteComment.php?id={$row->id}&amp;bug={$_GET["id"]}'>Delete</a></p>\n";

            print "</div>\n";
            print "<div class = 'spacer'></div>\n";
        }
    }
}
outputLogLinks( $_GET["id"] );
outputFooter();
?>
